package com.example.app.components.MyNotify

import android.util.Log
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import com.example.app.components.MyIcon.MyIcon
import com.example.app.components.MyListItem.MyListItem
import com.example.app.components.MyText.MyText
import com.example.app.components.MyTip.MyTipType
import com.example.app.pages.main.statusBarHeight
import com.example.app.ui.theme.my_danger
import com.example.app.ui.theme.my_primary
import com.example.app.ui.theme.my_success
import com.example.app.ui.theme.my_warning
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

/*
* 这个提示用在dialog里面
* 因为dialog的层级，zindex最高，用toast，或者其他，都不生效
* */
enum class MyNotifyType(
        var type: String,
        var color: Color,
        var icon: ImageVector
) {
    PRIMARY(
        "PRIMARY",
        my_primary,
        MyTipType.INFO.icon!!
    ),
    SUCCESS(
        "SUCCESS",
        my_success,
        MyTipType.SUCCESS.icon!!
    ),
    DANGER(
        "DANGER",
        my_danger,
        MyTipType.FAIL.icon!!
    ),
    WARNING(
        "WARNING",
        my_warning,
        MyTipType.INFO.icon!!
    ),
}
@OptIn(
    ExperimentalMaterial3Api::class,
    ExperimentalLayoutApi::class
)
@Composable
fun MyNotify(
        show: MutableState<Boolean>,
        type: MutableState<MyNotifyType> = mutableStateOf(MyNotifyType.SUCCESS),
        content: MutableState<String> = mutableStateOf(""),
        duration: MutableState<Duration> = mutableStateOf(4.seconds),
        closeHandler: () -> Unit = {}
) {
    var scope = rememberCoroutineScope()
    var showDp = TopAppBarDefaults.TopAppBarExpandedHeight + statusBarHeight
    var animateDp = animateDpAsState(targetValue = if (show.value) 0.dp else -showDp)

    Row(
        modifier = Modifier
            .fillMaxSize()
            .height(TopAppBarDefaults.TopAppBarExpandedHeight)
            .offset(y = animateDp.value)
            .background(type.value.color)
    ) {
        MyListItem(
            modifier = Modifier.fillMaxWidth(),
            colors = ListItemDefaults.colors(
                containerColor = Color.Transparent,
            ),
            hasWrap = false,
            leadingContent = {
                MyIcon(
                    imageVector = type.value.icon,
                    tint = MaterialTheme.colorScheme.surface
                )
            },
            headlineContent = {
                MyText(
                    text = "提示",
                    style = MaterialTheme.typography.bodyMedium.copy(
                        color = MaterialTheme.colorScheme.surface
                    )
                )
            },
            supportingContent = {
                MyText(
                    text = content.value,
                    style = MaterialTheme.typography.bodySmall.copy(
                        color = MaterialTheme.colorScheme.surface
                    )
                )
            }
        )
    }

    if (show.value) {
        scope.launch {
            delay(duration.value)
            show.value = false
            closeHandler()
        }
    }
}